#!/bin/bash

# 定义构建函数
build_app() {
  # 先清空./dist目录下的文件
  rm -rf ./dist/*

  local package_manager=$1
  local build_command=$2
  
  echo "运行 $package_manager install, $ENV"
  $package_manager install
  $package_manager run $build_command
}

# 定义部署函数
deploy_to() {
  local target_dir=$1
  
  # 判断目标目录是否存在,不存在则创建
  if [ ! -d "$target_dir" ]; then
    mkdir -p "$target_dir"
  fi
  
  # 删除目标目录下的文件
  rm -rf "$target_dir"/*
  
  # 拷贝文件到目标目录
  cp -r ./dist/* "$target_dir"
  
  echo "已部署到 $target_dir"
}

ENV=$1
echo "开始构建: $ENV "
echo "当前路径: $PWD"

if [ "$ENV" == "prod" ]; then
  build_app "cnpm" "build:prod"
  deploy_to "/home/docker/nginxwebui/wwwroot/platform-ui"
  
elif [ "$ENV" == "china-prod" ]; then
  build_app "npm" "build:prod"
  deploy_to "/home/nginxwebui/wwwroot/platform-ui-prod"
  
#  # 同时执行default环境部署
#  build_app "npm" "build"
#  deploy_to "/home/nginxwebui/wwwroot/platform-ui"
  
else
  build_app "npm" "build"
  deploy_to "/home/nginxwebui/wwwroot/platform-ui"
fi
